package Cau5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Client {
	public static void main(String[] args) throws SocketException, IOException {

        DatagramSocket sk = new DatagramSocket();
        DatagramPacket pk;
        String sResult = "";
        String sUserCommandInput = "";
        byte[] result;
        boolean cont = true;
        int port = 5000;
        Scanner scanner = new Scanner(System.in);
        InetAddress is = InetAddress.getLocalHost();

        while (cont) {

            System.out.print("Please enter your command (TIME, DATE, TRIPTIME, QUIT): ");
            sUserCommandInput = scanner.nextLine();
            sUserCommandInput = sUserCommandInput.toUpperCase();

            pk = new DatagramPacket(sUserCommandInput.getBytes(), sUserCommandInput.length(), is, port);
            sk.send(pk);

            if (sUserCommandInput.equals("QUIT")) {
                cont = false;
            } else {
                result = new byte[1024];
                pk = new DatagramPacket(result, result.length);
                sk.receive(pk);
                sResult = new String(pk.getData(), 0, pk.getLength());
                System.out.println(sResult);
            }
        }

        sk.close();
        System.out.println("Client terminated.");
    }
}
